Imports System.Drawing

Friend Class FMessage
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Cursor = System.Windows.Forms.Cursors.WaitCursor
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(40, 4)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(344, 32)
        Me.Label1.TabIndex = 1
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'FMessage
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.ClientSize = New System.Drawing.Size(424, 40)
        Me.Controls.Add(Me.Label1)
        Me.UseWaitCursor = True
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "FMessage"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.TopMost = True
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub FMessage_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Try
            Dim clsPen As Pen = New Pen(Color.Black)
            e.Graphics.DrawRectangle(clsPen, 0, 0, Me.Width - 1, Me.Height - 1)
            e.Graphics.DrawRectangle(clsPen, 1, 1, Me.Width - 3, Me.Height - 3)

            If mIcon1 IsNot Nothing Then e.Graphics.DrawImageUnscaled(mIcon1, mIcon1Rect)
            If mIcon2 IsNot Nothing Then e.Graphics.DrawImageUnscaled(mIcon2, mIcon2Rect)
        Catch
        End Try
    End Sub

    Public Sub SetMessage(ByVal text As String)
        Label1.Text = Text
        Me.Text = "Buddy 2 Buddy - " & text
    End Sub
    Public Sub SetRightIconSet(ByVal icons() As System.Drawing.Image)
        mRightAnimFrames = icons
        mRightAnimPos = 0
        mIcon2 = GetNextRightFrame()
        RedrawIcons()
    End Sub

    Private mIcon1 As Image
    Private mIcon1Rect As New Rectangle
    Private mIcon2 As Image
    Private mIcon2Rect As New Rectangle

    Private mLeftAnimFrames() As Image
    Private mLeftAnimPos As Integer = -1
    Private mRightAnimFrames() As Image
    Private mRightAnimPos As Integer = -1
    Private WithEvents mTimer As New Timers.Timer(500)

    Private Shadows Sub FMessage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mIcon1 = GetNextLeftFrame()
        mIcon2 = GetNextRightFrame()
        mTimer.Start()
    End Sub
    Public Shadows Sub Show(ByVal initialMessage As String, ByVal leftFrames() As Image, ByVal rightFrames() As Image)
        SetMessage(initialMessage)
        mLeftAnimFrames = leftFrames
        mRightAnimFrames = rightFrames
        MyBase.Show()
    End Sub

    Private Function GetNextLeftFrame() As Image
        If mLeftAnimFrames Is Nothing Then Return Nothing
        If mLeftAnimFrames.Count = 0 Then Return Nothing

        mLeftAnimPos += 1
        If mLeftAnimPos >= mLeftAnimFrames.Count Then mLeftAnimPos = 0
        Return mLeftAnimFrames(mLeftAnimPos)
    End Function
    Private Function GetNextRightFrame() As Image
        If mRightAnimFrames Is Nothing Then Return Nothing
        If mRightAnimFrames.Count = 0 Then Return Nothing

        mRightAnimPos += 1
        If mRightAnimPos >= mRightAnimFrames.Count Then mRightAnimPos = 0
        Return mRightAnimFrames(mRightAnimPos)
    End Function

    Private Sub FMessage_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Try
            mIcon1Rect = New Rectangle(4, 4, 32, 32)
            mIcon2Rect = New Rectangle(Me.Width - 32 - 4, 4, 32, 32)
        Catch
        End Try
    End Sub

    Private Sub mTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles mTimer.Elapsed
        Try
            mIcon1 = GetNextLeftFrame()
            mIcon2 = GetNextRightFrame()
            RedrawIcons()
        Catch
        End Try
    End Sub
    Private Sub RedrawIcons()
        Using g As Graphics = Me.CreateGraphics
            If mIcon1 IsNot Nothing Then g.DrawImageUnscaled(mIcon1, mIcon1Rect)
            If mIcon2 IsNot Nothing Then g.DrawImageUnscaled(mIcon2, mIcon2Rect)
        End Using
    End Sub

    Private Shadows Sub FMessage_Closing(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
        mTimer.Stop()
    End Sub
End Class
